package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.AnnualFeeStandard;
import com.lxw.nonghe.service.AnnualFeeStandardService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 年度缴费标准控制类
 */
@RestController
@RequestMapping("/annualFeeStandard")
@RequiresPermission("feeStandard:manage")
public class AnnualFeeStandardController {

    @Autowired
    private AnnualFeeStandardService feeStandardService;
    @Autowired
    private LogUtils logUtils;

    @PostMapping("/create")
    public Result<Boolean> addFeeStandard(@RequestBody AnnualFeeStandard feeStandard) {
        try {
            // 初始化创建时间和更新时间
            feeStandard.setCreateTime(new Date());
            feeStandard.setUpdateTime(new Date());

            boolean result = feeStandardService.save(feeStandard);
            if (result) {
                logUtils.recordLog("年度缴费标准管理", "新增标准", "成功",
                        "年份:" + feeStandard.getYear() + ", 金额:" + feeStandard.getAmount());
                return Result.success("年度缴费标准新增成功", true);
            } else {
                logUtils.recordLog("年度缴费标准管理", "新增标准", "失败", "保存数据库失败");
                return Result.error("年度缴费标准新增失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("年度缴费标准管理", "新增标准", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteFeeStandard(@PathVariable Integer id) {
        try {
            AnnualFeeStandard feeStandard = feeStandardService.getById(id);
            boolean result = feeStandardService.removeById(id);

            if (result) {
                logUtils.recordLog("年度缴费标准管理", "删除标准", "成功",
                        "记录ID:" + id + ", 年份:" + (feeStandard != null ? feeStandard.getYear() : "未知"));
                return Result.success("年度缴费标准删除成功", true);
            } else {
                logUtils.recordLog("年度缴费标准管理", "删除标准", "失败", "记录ID:" + id + "不存在");
                return Result.error("年度缴费标准删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("年度缴费标准管理", "删除标准", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updateFeeStandard(@RequestBody AnnualFeeStandard feeStandard) {
        try {
            // 更新时同步更新时间
            feeStandard.setUpdateTime(new Date());

            boolean result = feeStandardService.updateById(feeStandard);
            if (result) {
                logUtils.recordLog("年度缴费标准管理", "更新标准", "成功",
                        "记录ID:" + feeStandard.getStandardId());
                return Result.success("年度缴费标准更新成功", true);
            } else {
                logUtils.recordLog("年度缴费标准管理", "更新标准", "失败",
                        "记录ID:" + feeStandard.getStandardId() + "不存在");
                return Result.error("年度缴费标准更新失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("年度缴费标准管理", "更新标准", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public Result<AnnualFeeStandard> getFeeStandardById(@PathVariable Integer id) {
        try {
            AnnualFeeStandard feeStandard = feeStandardService.getById(id);
            if (feeStandard != null) {
                return Result.success("年度缴费标准查询成功", feeStandard);
            } else {
                logUtils.recordLog("年度缴费标准管理", "查询标准详情", "失败", "记录ID:" + id + "不存在");
                return Result.error("未找到该年度缴费标准");
            }
        } catch (Exception e) {
            logUtils.recordLog("年度缴费标准管理", "查询标准详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Page<AnnualFeeStandard>> getFeeStandardPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Byte status) {
        try {
            Page<AnnualFeeStandard> page = feeStandardService.getFeeStandardPage(
                    current, size, year, status);
            return Result.success("年度缴费标准分页查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("年度缴费标准管理", "分页查询标准", "失败", e.getMessage());
            return Result.error("分页查询异常: " + e.getMessage());
        }
    }

    // 新增条件查询方法
    @PostMapping("/query")
    public Result<Page<AnnualFeeStandard>> queryFeeStandards(
            @RequestBody AnnualFeeStandard queryParam,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<AnnualFeeStandard> page = feeStandardService.queryFeeStandards(
                    queryParam, current, size);
            return Result.success("年度缴费标准条件查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("年度缴费标准管理", "条件查询标准", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }
}
